home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Essentials / Technical.Notes / IIGS / TN.IIGS.081 < prev    next >
Encoding:
Text File  |  1991-06-28  |  16.4 KB  |  343 lines  |  [TEXT/pdos]

  1. Apple II
  2. Technical Notes
  3. _____________________________________________________________________________
  4.                                                   Developer Technical Support
  5.  
  6.  
  7. Apple IIgs
  8. #81:         Extended Control Ecstasy
  9.  
  10. Revised by:  Dave Lyons                                             July 1991
  11. Written by:  C.K. Haun                                               May 1990
  12.  
  13. This Technical Note discusses special features and concerns that should be 
  14. considered when using the extended controls introduced in System Software 
  15. 5.0.
  16.  
  17. Changes since November 1990:  Added information on which fields the Control 
  18. Manager automatically copies from a custom control's template to its control 
  19. record.  Corrected NewControl2 parameter order.  Added a note about 
  20. SetCtlTitle.  Changed some "pea 0" instructions into "pha" when pushing space 
  21. for results.
  22. _____________________________________________________________________________
  23.  
  24.  
  25. Introduction
  26.  
  27. The extended controls introduced in System Software 5.0 allow the application 
  28. programmer a great deal more freedom in designing and controlling 
  29. applications.  The new features enhance the functionality of the controls and 
  30. TaskMaster, but can cause confusion and consternation if you are careless 
  31. with the new parameter block .  This Note also includes a discussion of the 
  32. multipart nature of many of the extended controls and some pointers for 
  33. writing extended custom controls.
  34.  
  35. Counting The Costs
  36.  
  37. One of the major stumbling blocks seen when programming the new extended 
  38. controls is bad parameter counts.  Extended controls introduce parameter 
  39. blocks and parameter counts to the Control Manager.  You need to fully 
  40. understand the parameters required and the resulting parameter count for each 
  41. control you create, or you can experience program problems that may be very 
  42. confusing and difficult to track down.
  43.  
  44. Remember also that the Control Manager does not understand "skipping 
  45. parameters."  If you are creating an extended radio button and you want key 
  46. equivalents, but not a color table, you cannot ignore the color table 
  47. parameter field.  There is no way to tell the Control Manager to "skip" a 
  48. field during control creation; make sure you initialize all the fields to 
  49. values that are meaningful--either a real pointer, handle, resource ID, or 
  50. zeroes.  In this case, if you try to "skip" the color table and do not zero 
  51. out the color table parameter field, the resulting radio button wears ugly 
  52. colors you do not expect.
  53.  
  54. As you can imagine, miscounting parameters in other extended controls can 
  55. produce confusing results, so the parameter count is the first place you 
  56. should check when you're having difficulties creating extended controls.
  57.  
  58. For Rez users, the Types.Rez file contains extended control templates that 
  59. automatically generate the correct count value, so programmers creating all 
  60. their controls with the Resource Compiler should not have these problems.
  61.  
  62. Silly Little Bits
  63.  
  64. The other area of the new parameter block model that is giving folks trouble 
  65. is the moreflags field.  This field hones the definition given by the 
  66. reference fields and can cause you much grief if misused.  Make sure to set 
  67. the reference bits to the values you require.  The bit settings have been 
  68. standardized across all the extended controls, with %00 indicating a pointer, 
  69. %01 indicating a handle, and %10 indicating a resource.  Remember also to set 
  70. the bits for all the references you have--strings, color tables, or whatever 
  71. else may be ambiguously referenced in the control.
  72.  
  73. If you accidentally use the wrong bit pattern, you can experience strange 
  74. bugs ranging from garbled text to SysFailMgr caused by a nonexistent resource 
  75. being referenced.  Again, Rez users can use the equates for all the reference 
  76. specifiers in the Types.Rez file to avoid confusion and evil bugs.
  77.  
  78. The Parts are Greater than the Whole
  79.  
  80. To create some new extended controls, like pop-up menu controls and LineEdit 
  81. controls, functions of different tool sets were combined.  Pop-up menu 
  82. controls are a combination of the Control Manager and the Menu Manager, 
  83. LineEdit controls are a blending of the Control Manager and the LineEdit tool 
  84. set, and other new control types follow the same pattern.
  85.  
  86. This means that, at times, you have to go further into the documentation to 
  87. find information.  Getting the text out of an LineEdit extended control is a 
  88. multistep process that is a good example of this type of problem.
  89.  
  90. MyLineEdit dc     i2'8'                   ; parameter count
  91.            dc     i4'1'                   ; id number 1
  92.            dc     i2'10,10,23,90'         ; control rectangle
  93.            dc     i4'editLineControl'     ; process reference
  94.            dc     i2'0'                   ; flags
  95.            dc     i2'fCtlCanBeTarget+fCtlWantEvents+fCtlProcRefNotPtr'
  96.                                           ; moreflags
  97.            dc     i2'0'                   ; refcon
  98.            dc     i2'15'                  ; maximum characters allowed
  99.            dc     i4'0'                   ; no default text
  100. MyLineEditHandle ds 4                     ; handle for the created control
  101.  
  102.            pha
  103.            pha
  104.            pushlong mywindowgrafport      ;window that control will residein
  105.            pea    0                       ;verb, single Extended control
  106.            pushlong #MyLineEdit
  107.            _NewControl2
  108.            pla
  109.            sta    MyLineEditHandle        ; save the control handle
  110.            pla
  111.            sta    MyLineEditHandle+2
  112.  
  113.  
  114. When you want to get the text back out of that control later, you begin to 
  115. experience what it means to have a control that is an amalgam of various 
  116. tools.  You would start by using the control handle returned by NewControl2:
  117.  
  118. Scratch    equ    $0                      ; some scratch space
  119. Scratch2   equ    $4
  120.  
  121.            lda    MyLineEditHandle        ; move the ControlHandle to
  122.            sta    Scratch                 ; some direct page space
  123.            lda    MyLineEditHandle+2
  124.            sta    Scratch+2
  125.            lda    [Scratch]
  126.            tax
  127.            ldy    #2
  128.            lda    [Scratch],y             ; and dereference it, putting it 
  129.                                           ; back in some dpage
  130.            sta    Scratch+2               ; space to use it.
  131.            stx    Scratch
  132.  
  133. That gives you the pointer to the control record.  Stored in the control 
  134. record is the handle of the LineEdit item that is actually controlling the 
  135. text processing:
  136.  
  137.            pha                            ; make space for the text handle to 
  138.                                           ; be returned
  139.            pha
  140.            ldy    #octlData               ; offset to the ctlData section
  141.                                           ; of the ControlRecord
  142.            lda    [Scratch],y             ; where the handle for the actual    
  143.            tax                            ; LineEdit item was stored
  144.            iny
  145.            iny
  146.            lda    [Scratch],y
  147.            pha
  148.            phx
  149.            _LEGetTextHand                 ; ask for the handle for the text 
  150.            pla                            ; in this LineEdit control
  151.            sta    Scratch2                ; and now you have the handle to
  152.                                           ; the text you want.
  153.            pla
  154.            sta    Scratch2+2
  155.  
  156. The main point is that when you are using extended controls, you often cannot 
  157. use the Control Manager to do everything that needs to be done.  You also 
  158. need to understand and use the supplementary or "hidden" tool sets.
  159.  
  160. Here's another example, using a pop-up menu extended control, and in this 
  161. case we define a font pop-up that contains all the font names currently 
  162. available.
  163.  
  164. MyPopUpControl dc i2'9'                   ; parameter count of 9 
  165.            dc    i4'1'                    ; control ID of 1
  166.            dc    i2'2,2,0,0'              ; Position, upper left corner of 
  167.                                           ; the window, let
  168.                                           ; Control Manager calculate full
  169.                                           ; size
  170.            dc    i4'popUpControl'         ; def proc for PopUp
  171.            dc    i2'0'                    ; flags
  172.            dc    i2'fCtlWantEvents+fCtlProcRefNotPtr'
  173.                                           ; more flags
  174.            dc    i4'0'                    ; ref con
  175.            dc    i2'0'                    ; title width, will be calculated
  176.            dc    i4'mymenu'               ; pointer to actual menu structure
  177.            dc    i2'500'                  ; initial value, item number of
  178.                                           ; item to be displayed in popup at 
  179.                                           ; creation
  180.  
  181.  
  182. mymenu     dc    i2'0'                    ; version number, should be 0
  183.            dc    i2'200'                  ; menu ID number
  184.            dc    i2'0'                    ; menu flags
  185.            dc    i4'mymenutitle'          ; pointer to menu title
  186.            dc    i4'mymenuitem1'          ; first menu item
  187.            dc    i4'mymenuitem2'          ; second menu item
  188.            dc    i4'0'                    ; null terminator, end of menu
  189. mymenutitle str 'Font'
  190.  
  191. mymenuitem1 dc i2'0'                      ; version number
  192.            dc i2'500'                     ; item number
  193.            dc i2'0'                       ; no hot keys
  194.            dc i2'0'                       ; not checked
  195.            dc i2'0'                       ; item flags, no special drawing
  196.            dc i4'mymenuitem1title'
  197. mymenuitem1title str 'Plain'
  198.  
  199. mymenuitem2 dc i2'0'                      ; version number
  200.            dc i2'501'                     ; item number
  201.            dc i2'0'                       ; no hot keys
  202.            dc i2'0'                       ; not checked
  203.            dc i2'1'                       ; item flags, bold face this one
  204.            dc i4'mymenuitem2title'
  205. mymenuitem2title str 'Bold'
  206.  
  207. Now  create this control:
  208.  
  209.            pha
  210.            pha
  211.            pushlong mywindow              ; target window grafptr
  212.            pea    0                       ; verb, single control pointer
  213.            pushlong #MyPopUpMenu
  214.            _NewControl2
  215.            pulllong mypopuphandle         ; save the handle
  216.  
  217. This pop-up menu control created is not associated with the menu bar across 
  218. the top of the desktop.  You can consider each of your pop-up menu controls 
  219. as separate menu bars, so if you want to perform Menu Manager calls on a 
  220. pop-up menu control, you need to set the menu to point at your pop-up menu 
  221. control.  In this example, to add all the fonts available to the pop-up menu 
  222. you would:
  223.  
  224.            pha
  225.            pha                            ; space to hold current bar
  226.            _GetMenuBar                    ; get the handle to the current
  227.                                           ; menu bar
  228.            pushlong mypopuphandle
  229.            _SetMenuBar
  230.            pea    200                     ; id number of this menu
  231.            pea    502                     ; first font family ID number to
  232.                                           ; use
  233.            pea    0                       ; fontspecbits
  234.            _FixFontMenu
  235.            pea    0
  236.            pea    0
  237.            pea    200
  238.            _CalcMenuSize                  ; re-size the popup menu
  239.            _SetMenuBar                    ; restore the previous menu as the 
  240.                                           ; current menu
  241.  
  242.  
  243. Controls That Are Not Controls
  244.  
  245. The new picture extended control is not a "full-fledged" control; it has been 
  246. provided to simplify your programming tasks.  The picture control does not 
  247. support normal mouse hit testing and highlighting.  Think of it as a built-in 
  248. extension to your content drawing routine, and not as a control.  It is 
  249. provided to allow you to refresh your whole window with a single DrawControls 
  250. call, instead of drawing the controls and then drawing pictures.  The icon 
  251. button extended control has been provided as the graphic full-function 
  252. control.  If you need or want a fully functional control that uses a picture, 
  253. you should consider writing your own custom control procedure.
  254.  
  255. Custom Extended Controls
  256.  
  257. Custom controls can also benefit from all the advantages of extended 
  258. controls.  You can create a custom control that uses a template, can be a 
  259. resource, has a definition procedure that is a resource, and responds to all 
  260. the new control calls.  If you write an extended custom control or upgrade a 
  261. previously-written custom control, there are new messages and changes to 
  262. existing messages of which you need to be aware.  These changes are 
  263. documented in volume 3 of the Apple IIgs Toolbox Reference. 
  264.  
  265. The Control Manager copies the following fields from the control template to 
  266. the control record before it sends your control the init message:  ctlOwner, 
  267. ctlID, ctlRect, ctlFlag, ctlHilite, ctlMoreFlags, ctlVersion, ctlRefCon, and 
  268. ctlProc.  The ctlNext field is owned by the Control Manager.  If any 
  269. additional fields need to be set up based on the control template (such as 
  270. ctlValue, ctlData, ctlColor, and any custom fields), your init routine needs 
  271. to take care of it.
  272.  
  273. Putting your custom control definition procedure in a resource can 
  274. significantly enhance the functionality of the custom control.  You may find 
  275. it easier to add to all of your programs and you do not have to manage the 
  276. code space required. If you do write a custom control definition procedure 
  277. and want to store it as a resource, here are some hints for success.
  278.  
  279. First, the code you store in your resource fork must be fully compiled and 
  280. linked code.  The code resource converter uses the System Loader to load the 
  281. code, so the code must be executable code, not object code.
  282.  
  283. Second, set the convert and locked bits of the resource attributes for your 
  284. code resource.  The convert bit must be set to tell the Resource Manager to 
  285. call the code resource converter when it loads this resource. The resource 
  286. type for control definition procedures is rCtlDefProc, $800C.
  287.  
  288. By setting locked but not fixed, memory fragmentation is reduced (because of 
  289. how the code resource converter and Memory Manager work).  Setting the locked 
  290. attribute is also recommended for compatibility with future system software.
  291.  
  292. Third, keep in mind that this definition procedure may be purged and reloaded 
  293. whenever the Memory Manager needs the space.  This means that you cannot 
  294. store any information in your definition procedure if you want to keep track 
  295. of it between calls to the definition procedure.  If you do, and your 
  296. definition procedure gets purged and reloaded, you lose that data.
  297.  
  298. If you need data space for your custom control, use the control record as 
  299. your stash.  You can easily either use the fields already provided in the 
  300. control record, or you can expand the control record to as much space as you 
  301. need (within sensible limits) and store your data there.
  302.  
  303. Warning:    Control definition procedures are initially loaded with purge 
  304. level zero.  When they are released, they are given purge level three.  If 
  305. they are then reloaded, the Resource Manager does not change the purge level 
  306. back to zero--your definition procedure may then be purged (even while 
  307. executing) unless its handle is locked.  The solution is to lock your 
  308. definition procedure handle within the procedure:
  309.  
  310.                    myPosition    pea    0                ; space for result
  311.                                  pea    0
  312.                                  pushLong #myPosition
  313.                                  _FindHandle
  314.                                  _HLock
  315.  
  316.     and unlock your handle with HUnlock on exit.  This keeps your procedure 
  317. safe, while not creating "code islands," which clog up memory.
  318.  
  319.  
  320. Changing a Control's Title
  321.  
  322. If you call SetCtlTitle to give a control a new title, everything is great if 
  323. the new title is referenced the same way as the current title (by pointer, by 
  324. handle, or by resource ID).  If the new title is referenced differently, you 
  325. must first call SetCtlMoreFlags on your control so that the SetCtlTitle value 
  326. can be interpreted correctly.
  327.  
  328.  
  329. Conclusion
  330.  
  331. The extended controls provided in System Software 5.0 and later are a great 
  332. leap forward for programmers.  They relieve the application of much of the 
  333. tedious detail code that relates to housekeeping, not the guts of application 
  334. programming.  Used in combination with the enhanced TaskMaster, you can have 
  335. an application's visual interface up and running a lot faster, leaving you 
  336. more time to work on the heart of your application.
  337.  
  338.  
  339. Further Reference
  340. _____________________________________________________________________________
  341.   o  Apple IIgs Toolbox Reference, Volumes 1-3.
  342.  
  343.